package ualrcdg.client;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;

/**
 * This method fetches the game selected from
 * this UI.
 * Continues from {@link LoginActivity}
 */
public class GameFetcherActivity extends Activity {

	private CommunicatorService mBoundService;
    private Boolean mIsBound;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.createprofilelayout);
		
		final GameFetcherActivity gf = this;
		
		final RadioGroup GameGroup = (RadioGroup) findViewById(R.id.GameSelectionGroup);
		final Button Submit = (Button) findViewById(R.id.button1);
		
		
		Submit.setOnClickListener(new View.OnClickListener() 
		{
			/**
        	 * This method is called when the button on the interface is pressed.
        	 * When executed this method will select the game from the CDG
        	 * server and launch it in the next activity.
        	 * 
        	 * (non-Javadoc)
        	 * @see android.view.View.OnClickListener#onClick(android.view.View)
        	 */
	        public void onClick(View v) 
	        {
	            	//
	            	//Check Username and Password
	            	//
	            	int selectedId = GameGroup.getCheckedRadioButtonId();
	            	RadioButton GameSelection = (RadioButton) findViewById(selectedId);
	            	String Game = GameSelection.getText().toString();
	            	startActivity(new Intent(gf, ualrcdg.client.FakeGameActivity.class).putExtra("Game", Game));
	         }
        }); 
	}
	
	private ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {
            mBoundService = ((CommunicatorService.LocalBinder)service).getService();

        }
        public void onServiceDisconnected(ComponentName className) {
            mBoundService = null;
        }
    };

    private void doBindService() {
        bindService(new Intent(GameFetcherActivity.this, CommunicatorService.class), mConnection, Context.BIND_AUTO_CREATE);
        mIsBound = true;
        mBoundService.IsBoundable();
    }


    private void doUnbindService() {
        if (mIsBound) {
            // Detach our existing connection.
            unbindService(mConnection);
            mIsBound = false;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        doUnbindService();
    }
}
